﻿@model IEnumerable<TagGroup>
@{
    Layout = "_ControlPanel";
}

<div class="tn-block tnc-ctnborder">
    <div class="tnc-main">
        <div class="tnc-rightvar tn-open">
            <div class="tnc-settings-icon" plugin="ShortcutMenu">
                <a href="#"></a>
            </div>
            <div id="ManageUserRightMenu">
                @Html.Partial("_ManageMicroblogRightMenu")
            </div>
        </div>
        <div class="tnc-ctn-center">
            <h1>
                <span>话题分组管理</span></h1>
            <div class="tnc-ctnright-list">
                <div class="tnc-content-manage">
                    <div class="tn-list-header">
                        <div class="tn-toolbar-area">
                            @Html.LinkButton("创建", SiteUrls.Instance()._EditTopicGroup(), iconType: IconTypes.SmallAdd, htmlAttributes: new { plugin = "dialog" })
                            @Html.LinkButton("删除", SiteUrls.Instance().DeleteTopicGroups(), iconType: IconTypes.SmallCross, htmlAttributes: new { id = "delete-topic-groups" })
                        </div>
                    </div>
                    @if (Model != null && Model.Count() == 0)
                    { 
                        <div class="tn-no-data">
                            没有查询到数据！</div>
                    }
                    else if (Model != null)
                    {
                        <table class="tn-table-grid">
                            <tbody>
                                <tr class="tn-table-grid-header">
                                    <th class="tn-border-gray tn-border-bottom tn-text-note">
                                        <input type="checkbox" id="select-all" />
                                    </th>
                                    <th class="tn-border-gray tn-border-bottom tn-text-note">
                                        话题分组名
                                    </th>
                                    <th class="tn-border-gray tn-border-bottom tn-text-note">
                                        操作
                                    </th>
                                </tr>
                                @foreach (var group in Model)
                                { 
                                    <tr class="tn-table-grid-row">
                                        <td class="tn-border-gray tn-border-bottom tn-width-check">
                                            @Html.CheckBox("group-id", new { @class = "tnui-group-id", value = @group.GroupId })
                                        </td>
                                        <th class="tn-border-gray tn-border-bottom tn-width-auto">
                                            @group.GroupName
                                        </th>
                                        <td class="tn-border-gray tn-border-bottom tn-width-action2 tn-nowrap">
                                            @Html.Link("编辑", SiteUrls.Instance()._EditTopicGroup(group.GroupId), "编辑", htmlAttributes: new { plugin = "dialog" })
                                            @Html.Link("删除", SiteUrls.Instance().DeleteTopicGroups(group.GroupId.ToString()), "删除该分组", new { @class = "tn-action-text tnui-delete-group", id = "delete-group" })
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        //处理火狐下浏览器刷新后复选框仍然选中
        removechecked();
        function removechecked() {
            $(".tnui-group-id").each(function () {
                if ($(this).attr("checked")) {
                    $(this).removeAttr("checked");
                }
            });
            $("#select-all").removeAttr("checked");
        }
        //给当前行添加样式
        $(".tn-table-grid-row").hover(
            function () {
                $(this).addClass("tn-bg-gray");
            },
            function () {
                $(this).removeClass("tn-bg-gray");
            }
        );

        //组合所有选中的复选框的只，以,分隔
        function getCheckedGroupIds() {
            var topicGroupIds = "";
            $(".tnui-group-id").each(function () {
                if ($(this).attr("checked")) {
                    topicGroupIds += $(this).val() + ",";
                }
            });
            return topicGroupIds;
        }

        // 全选 全不选
        $("#select-all").click(function () {
            //设置下面复选框的状态
            if ($(this).attr("checked")) {
                $(".tnui-group-id").each(function () {
                    $(this).attr("checked", true);
                });
            } else {
                $(".tnui-group-id").each(function () {
                    $(this).removeAttr("checked");
                });
            }
        });

        //点批量删除
        $("#delete-topic-groups").click(function (e) {
            e.preventDefault();

            var topicGroupIds = getCheckedGroupIds();
            if (!topicGroupIds) {
                alert("请至少选择一个分组！");
                return;
            }

            var url = $(this).attr("href");
            art.dialog.confirm('您确认要删除吗？', function () {
                $.getJSON(url, { topicGroupIds: topicGroupIds }, function (data) {
                    if (data) {
                        art.dialog.tips(data.MessageContent, 1.5, data.MessageType);
                        setTimeout(function () { window.location.reload(true); }, 500);
                    }
                });
                removechecked();
            });
        });

        //单个删除
        $('.tnui-delete-group').click(function (e) {
            e.preventDefault();

            var url = $(this).attr("href");
            art.dialog.confirm('您确认要删除吗？', function () {
                $.getJSON(url, function (data) {
                    if (data) {
                        art.dialog.tips("删除成功！", 1.5, 1);
                        setTimeout(function () { window.location.reload(true); }, 500);
                    }
                });
            });
        });
    });
</script>
